//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using ViNoToolkit;

/// <summary>
/// Dialog part node utility.
/// </summary>
static public class DialogPartNodeUtility{

		static private SceneEvent m_SceneEvent;
		static private SceneEvent sceneEvent{
			get{
				if( m_SceneEvent == null ){
						SceneEvent se = GameObject.FindObjectOfType( typeof(SceneEvent )) as SceneEvent;
						m_SceneEvent = se;
				}
				return m_SceneEvent;
			}
		}

		/// <summary>
		/// Finds the Name Text Box and set to DialogPartNode.
		/// </summary>
		/// <param name="node">Node.</param>
	static public void FindNameTextBox( DialogPartNode node ){
		GameObject nameTextBox = GameObject.Find( "TextBox_Name");
		if( nameTextBox != null ){
			 node.m_NameTextBox = nameTextBox.GetComponent<ViNoTextBox>();
			EditorGUIUtility.PingObject( node.m_NameTextBox.gameObject );
		}
	}

		/// <summary>
		/// Finds the dialog text box.
		/// </summary>
		/// <param name="node">Node.</param>
	static public void FindDialogTextBox( DialogPartNode node ){
		GameObject textBox = GameObject.Find( "TextBox");
		if( textBox != null ){
			node.m_ViNoTextBox = textBox.GetComponent<ViNoTextBox>();
			EditorGUIUtility.PingObject( node.m_ViNoTextBox.gameObject );
		}
	}

		/// <summary>
		/// Views the dialog.
		/// </summary>
		/// <param name="data">Data.</param>
		/// <param name="dialogTextBox">Dialog text box.</param>
		/// <param name="nameTextBox">Name text box.</param>
	static public void ViewDialog( DialogPartData data , ViNoTextBox dialogTextBox , ViNoTextBox nameTextBox ){
		if( dialogTextBox != null ){
			dialogTextBox.SetText( data.dialogText );
		}
		if( nameTextBox != null ){
			nameTextBox.SetText( data.nameText );
		}	
	}

		/// <summary>
		/// Clears the actors.
		/// </summary>
		/// <param name="actorLib">Actor lib.</param>
	static public void ClearActors( ActorLibrary actorLib ){
		foreach( ActorInfo actor in actorLib.actorEntries ){
			bool immediate = true;
			sceneEvent.TriggerExitActor( actor.actorName , immediate );			
		}
	}

		/// <summary>
		/// Updates the game view.
		/// </summary>
		/// <param name="data">Data.</param>
	static public void UpdateGameView( DialogPartData data ){		
		if( data.scene == null ){
			return;
		}
						
		switch( data.actionID ){
			case DialogPartNodeActionType.EnterScene:
				if( sceneEvent.sceneLib != null ){
					Scene sceneData = sceneEvent.sceneLib.GetSceneBy( data.scene.sceneName );
					if( sceneData != null ){
						SceneCreator.CreateScene( sceneData , false );
					}
				}
				break;

			case DialogPartNodeActionType.ExitScene:
				ViNoSceneManager sm = GameObject.FindObjectOfType(typeof(ViNoSceneManager)) as ViNoSceneManager;
				SceneCreator.DestroyScene( sm.theSavedPanel , ! false );	// Fade and Destroy Actor.
				break;

			case DialogPartNodeActionType.EnterActor:
				if( data.enterActorEntries != null && data.enterActorEntries.Length > 0 ){
//					ClearActors( sceneEvent.actorLib );
					foreach( DialogPartData.ActorEntry ent in data.enterActorEntries ){
						sceneEvent.TriggerEnterActor( ent.actorName , ent.position );
					}
				}
				break;

			case DialogPartNodeActionType.MoveActor:
				if( data.enterActorEntries != null && data.enterActorEntries.Length > 0 ){
//					ClearActors( sceneEvent.actorLib );
					foreach( DialogPartData.ActorEntry ent in data.enterActorEntries ){
						sceneEvent.MoveObject( ent.actorName , ent.position.ToString() );
					}
				}
				break;

			case DialogPartNodeActionType.ExitActor:
				if( data.exitActorEntries != null && data.exitActorEntries.Length > 0 ){
					foreach( DialogPartData.ActorEntry ent in data.exitActorEntries ){
						bool immediate = true;
						sceneEvent.TriggerExitActor( ent.actorName , immediate );
					}
				}
				break;
		}
	}

		/// <summary>
		/// Imports the text.
		/// </summary>
		/// <param name="dlg">Dlg.</param>
		/// <param name="text">Text.</param>
		/// <param name="blockDelimiter">Block delimiter.</param>
		/// <param name="nameDelimiter">Name delimiter.</param>
		/// <param name="overWrite">If set to <c>true</c> over write.</param>
	static public void ImportText( DialogPartNode dlg , string text , string blockDelimiter , string[] nameDelimiter , bool overWrite ) {
		string[] delimiter = { blockDelimiter }; //{ System.Environment.NewLine + System.Environment.NewLine };
		string[] blocks = text.Split(delimiter, System.StringSplitOptions.None );

		int dlgID = 0;
		if( dlg != null ){
			if( overWrite ){
				dlg.dlgDataList.Clear();
			}
			foreach (string s in blocks) {
			  string[] temp = s.Split( nameDelimiter, System.StringSplitOptions.None );
			  if( temp.Length > 1 ){
				DialogPartData data = new DialogPartData();
				data.dialogID = dlgID;
				data.nameText = temp[ 0 ];
				data.dialogText = temp[ 1 ];
				data.isName = true;
				dlg.AddData( data );
			  }
			  else{
				  dlg.AddData( "" , s );
			  }
			  dlgID++;
			}
//			Debug.Log( "Imported Text."); 
		}
	}

		/// <summary>
		/// Initialize the specified DialogPartNode.
		/// </summary>
		/// <param name="dlg">Dlg.</param>
	static public void Initialize( DialogPartNode dlg ){		
		dlg.dlgDataList = new List<DialogPartData>();
		dlg.dlgDataList.Add( new DialogPartData( ) );
	}

		/// <summary>
		/// Swap2s the items.
		/// </summary>
		/// <param name="targetNode">Target node.</param>
	static public void Swap2Items( DialogPartNode targetNode ){
		int idx1 = -1;
		int idx2 = -1;
		for( int i=0;i<targetNode.dlgDataList.Count;i++){
			if( targetNode.dlgDataList[ i ].active ){
				if( idx1 == -1 ){
					idx1 = i;
				}
				else if( idx2 == -1 ){
					idx2 = i;
					break;
				}
			}
		}

		if( idx1 != -1 && idx2 != -1 ){
			DialogPartData data1 = targetNode.dlgDataList[ idx1 ];
			DialogPartData data2 = targetNode.dlgDataList[ idx2 ];

			targetNode.dlgDataList[ idx1 ] = data2;
			targetNode.dlgDataList[ idx2 ] = data1;			
		}
		else{
			Debug.Log( "please check 2 items when swap items.");
		}
	}	

		/// <summary>
		/// Exports the scenario script.
		/// </summary>
		/// <param name="targetNode">Target node.</param>
	static public void ExportScenarioScript( DialogPartNode targetNode ){
//		int num = targetNode.GetMessageNum();
		System.Text.StringBuilder sb = new System.Text.StringBuilder();
		targetNode.ToScenarioScript( ref sb );

//	    TextAsset text = new TextAsset();
//	    AssetDatabase.CreateAsset(text, "Assets/" + targetNode.name + ".txt" );
//		text.text = sb.ToString();
//		AssetDatabase.SaveAssets();	    

		File.WriteAllText(Application.dataPath + "/" + targetNode.name + ".txt", sb.ToString() ) ;
		AssetDatabase.Refresh();
		Debug.Log ( sb.ToString() );
	}

}
